<?php

namespace App\Dao;

use App\Exceptions\ApiErrorException;
use EasyWeChat\Factory;
use App\Models\Payments;
/**
 * 日志记录调用模型
 *
 * @author yh 20201215
 */
class SendDao
{
    /**
     *  退款成功通知客户
     *  @author yh 20201215
     * */ 
    public static function refund_msg($openid,$url,$first,$reason,$refund,$remark){
        $payment_id = PAYMENT_ID_WXPAY;
        $payment = Payments::findOrFail($payment_id)->first();
        $payment_info =  unserialize(preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$payment->payment_info));
        $config = [
            'app_id' =>  $payment_info['wxAppId'],//公众号app_id
            'secret' => '35acfd6b84e18a1bed203cce17528c33',//公众号secret
            'log'    => [ //生成错误日志
                        'level' => 'debug',
                        'file' => '/tmp/esaywechat.log',
                ]
        ];
        $app = Factory::officialAccount($config);
        $app->template_message->send([
            'touser' => $openid,//用户openid
            'template_id' => '-5lJ3wLBlaB1UqHesr311d2Wt2a0zSxNP1tcADWkiUg',//发送的模板id
            'url' => $url,//发送后用户点击跳转的链接
            
            'data' => [
                'first' => $first,
                'reason' => $reason,
                'refund' => $refund,
                'remark'=>$remark
            ],
        ]);
    }
    /**
     *  拼团成功通知
     *  @author yh 20201215
     * 
    **/
    public static function join_msg($openid,$url,$first,$reason,$refund,$remark){
        $payment_id = PAYMENT_ID_WXPAY;
        $payment = Payments::findOrFail($payment_id)->first();
        $payment_info =  unserialize(preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$payment->payment_info));
        $config = [
            'app_id' =>  $payment_info['wxAppId'],//公众号app_id
            'secret' => '35acfd6b84e18a1bed203cce17528c33',//公众号secret
            'log'    => [ //生成错误日志
                        'level' => 'debug',
                        'file' => '/tmp/esaywechat.log',
                ]
        ];
        $app = Factory::officialAccount($config);
        $app->template_message->send([
            'touser' => $openid,//用户openid
            'template_id' => '69Z87j1EWHZoPC1sKqHNSrynsV9VV5LVTqWzqXMgQJo',//发送的模板id
            'url' => $url,//发送后用户点击跳转的链接
            
            'data' => [
                'first' => $first,
                'keyword1' => $reason,
                'refund' => $refund,
                'remark'=>$remark
            ],
        ]);
    }
}